package cn.zhengzhou.springbootmybatis.controller;

import cn.zhengzhou.springbootmybatis.mapper.StudentsMapper;
import cn.zhengzhou.springbootmybatis.pojo.ResultVo;
import cn.zhengzhou.springbootmybatis.pojo.User;
import cn.zhengzhou.springbootmybatis.service.UserService;
import cn.zhengzhou.springbootmybatis.service.impl.UserServiceImpl;
import cn.zhengzhou.springbootmybatis.utils.JwtUtil;
import cn.zhengzhou.springbootmybatis.utils.ProxyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/login")
    public ResultVo<Object> login(@RequestParam String username,@RequestParam String password) throws Exception {
        log.info("前端请求参数：{}",username);
        //动态代理方法执行时间
        UserService userService1 = ProxyUtil.createProxy(userService);
        Thread.sleep(1500);
        User user = userService1.findUser(username, password);
        System.out.println(user);
        if (user == null) {
            return ResultVo.fail(500,"登录失败",user);
        }
        String token = jwtUtil.createToken(user);
        return ResultVo.success(token);
    }

}
